package org.brain.bean;

import java.util.Date;


import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.brain.dbo.DBOperate;
import org.brain.pojos.Document;
import org.brain.pojos.Documenttype;
import org.brain.pojos.Treemenu;
import org.brain.pojos.User;
import org.brain.tool.ToolBean;

@ManagedBean(name="documentBean")
@RequestScoped

public class documentBean {
	private Integer documentid;
	@ManagedProperty(value = "#{documentType}")
	private documentTypeBean documenttype;
	
	@ManagedProperty(value =  "#{selectBean}")
	private SelectBean treemenu;
	
	private String author;
	private Date years;
	private String title;
	private String link;
	private String secondauthor;
	private String secondtitle;
	private String placeofpublication;
	private String press;
	private String serialnumber;
	private Integer pagenumber;
	private String referencescited;
	private double version;
	private Date date;
	private String simpletitle;
	private String isbn;
	private String classify;
	private String label;
	private String keywords;
	private String themewords;
	private String summary;
	private String note;
	private String image;
	private String language;
	private Date lastsearchtime;
	private Date createtime;
	private Date edittime;
	private String message;
	private String filePath;
	private boolean insertFlag;
	private Integer sharetg;

	// Constructors

	/** default constructor */
	public documentBean() {
	}

	public Integer getDocumentid() {
		return this.documentid;
	}

	public void setDocumentid(Integer documentid) {
		this.documentid = documentid;
	}

	public documentTypeBean getDocumenttype() {
		return documenttype;
	}

	public void setDocumenttype(documentTypeBean documenttype) {
		this.documenttype = documenttype;
	}

	public SelectBean getTreemenu() {
		return this.treemenu;
	}

	public void setTreemenu(SelectBean treemenu) {
		this.treemenu = treemenu;
	}

	public String getAuthor() {
		return this.author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getYears() {
		return this.years;
	}

	public void setYears(Date years) {
		this.years = years;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getLink() {
		return this.link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public String getSecondauthor() {
		User user;
		user = ToolBean.getUserSession();
		secondauthor=user.getUsername();
		return this.secondauthor;
	}

	public void setSecondauthor(String secondauthor) {
		this.secondauthor = secondauthor;
	}   

	public String getSecondtitle() {
		return this.secondtitle;
	}

	public void setSecondtitle(String secondtitle) {
		this.secondtitle = secondtitle;
	}

	public String getPlaceofpublication() {
		return this.placeofpublication;
	}

	public void setPlaceofpublication(String placeofpublication) {
		this.placeofpublication = placeofpublication;
	}

	public String getPress() {
		return this.press;
	}

	public void setPress(String press) {
		this.press = press;
	}

	public String getSerialnumber() {
		return this.serialnumber;
	}

	public void setSerialnumber(String serialnumber) {
		this.serialnumber = serialnumber;
	}

	public Integer getPagenumber() {
		return this.pagenumber;
	}

	public void setPagenumber(Integer pagenumber) {
		this.pagenumber = pagenumber;
	}

	public String getReferencescited() {
		return this.referencescited;
	}

	public void setReferencescited(String referencescited) {
		this.referencescited = referencescited;
	}

	public double getVersion() {
		return this.version;
	}

	public void setVersion(double version) {
		this.version = version;
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getSimpletitle() {
		return this.simpletitle;
	}

	public void setSimpletitle(String simpletitle) {
		this.simpletitle = simpletitle;
	}

	public String getIsbn() {
		return this.isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getClassify() {
		return this.classify;
	}

	public void setClassify(String classify) {
		this.classify = classify;
	}

	public String getLabel() {
		return this.label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getKeywords() {
		return this.keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getThemewords() {
		return this.themewords;
	}

	public void setThemewords(String themewords) {
		this.themewords = themewords;
	}

	public String getSummary() {
		return this.summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getNote() {
		return this.note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public String getImage() {
		return this.image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getLanguage() {
		return this.language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public Date getLastsearchtime() {
		return this.lastsearchtime;
	}

	public void setLastsearchtime(Date lastsearchtime) {
		this.lastsearchtime = lastsearchtime;
	}

	public Date getCreatetime() {
		return this.createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	public Date getEdittime() {
		return this.edittime;
	}

	public void setEdittime(Date edittime) {
		this.edittime = edittime;
	}
	
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getMessage() {
		
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
	public boolean isInsertFlag() {
		return insertFlag;
	}

	public void setInsertFlag(boolean insertFlag) {
		this.insertFlag = insertFlag;
	}
	
	public Integer getSharetg() {
		return this.sharetg;
	}

	public void setSharetg(Integer sharetg) {
		this.sharetg = sharetg;
	}

	public String InsertDocument()
	{
		
		FacesContext facesContext = FacesContext.getCurrentInstance();  
		 documenttype = (documentTypeBean) facesContext.getApplication()  
      		.getVariableResolver().resolveVariable(facesContext, "documentType");
      	  treemenu = (SelectBean) facesContext.getApplication().getVariableResolver()
      	  	.resolveVariable(facesContext, "selectBean");
		boolean flag=true;
		System.out.println("treeMenu "+treemenu.getCurrentType1());
		System.out.println("documenttype "+documenttype.getTypeId());
		
		
		
		Treemenu treeMenu = DBOperate.getTreeMenu(Integer.parseInt(treemenu.getCurrentType1()));
		Documenttype documentType = DBOperate.getDocumenttype(Integer.parseInt(documenttype.getTypeId())); 
		
		Document document = new Document();
		
		document.setTreemenu(treeMenu);
		document.setDocumenttype(documentType);	
		document.setAuthor(author);
		document.setTitle(title);
		document.setKeywords(keywords);
		document.setThemewords(themewords);
		document.setSummary(summary);
		document.setClassify(classify);
		
		document.setCreatetime(new Date());
		document.setYears(years);
		//修改用户名称
		User user = ToolBean.getUserSession();
		document.setLink("file/"+user.getUsername()+"/"+link);
		document.setSecondauthor(user.getUsername());
		document.setSecondtitle(secondtitle);
		document.setPlaceofpublication(placeofpublication);
		document.setPress(press);
		document.setSerialnumber(serialnumber);
		document.setPagenumber(pagenumber);
		document.setReferencescited(referencescited);
		document.setVersion(version);
		document.setDate(date);
		document.setSimpletitle(simpletitle);
		document.setIsbn(isbn);
		document.setLabel(label);
		document.setNote(note);
		document.setImage("pic/"+user.getUsername()+"/"+image);
		document.setLanguage(language);
		document.setLastsearchtime(new Date());
		document.setEdittime(edittime);
	
		System.out.println("this is a test panel!");
		insertFlag = DBOperate.save(document);
		return "/documentAddResult";
			
	}
	
	public boolean delDoucment()
	{
		boolean flag = false;
		System.out.println(documentid);
		System.out.println("delete start....");
		Document doc = DBOperate.getDocument(documentid);
		flag = DBOperate.del(doc);
		System.out.println("delete end...");
		
		return flag;
	}
	public boolean shareDoucment()
	{
		boolean flag = false;
		System.out.println(documentid);
		System.out.println("share start....");
		Document doc = DBOperate.getDocument(documentid);
		doc.setSharetg(1);
		System.out.println("share end...");
		DBOperate.update(doc);
		return flag;
	}
	public boolean calshareDoucment()
	{
		boolean flag = false;
		System.out.println(documentid);
		System.out.println("share start....");
		Document doc = DBOperate.getDocument(documentid);
		doc.setSharetg(0);
		System.out.println("share end...");
		DBOperate.update(doc);
		return flag;
	}
	
	public void initdoc_update()
	{
		documentid = Integer.parseInt(ToolBean.getRequestParameter("id"));
		Document doc = DBOperate.getDocument(documentid);
		author = doc.getAuthor();
		years = doc.getYears();
		title = doc.getTitle();
		link = doc.getLink();
		secondauthor = doc.getSecondauthor();
		secondtitle = doc.getSecondtitle();
		placeofpublication = doc.getPlaceofpublication();
		press = doc.getPress();
		serialnumber = doc.getSerialnumber();
		pagenumber = doc.getPagenumber();
		referencescited = doc.getReferencescited();
		version = doc.getVersion();
		date = doc.getDate();
		simpletitle = doc.getSimpletitle();
		isbn = doc.getIsbn();
		classify = doc.getClassify();
		label = doc.getLabel();
		keywords = doc.getKeywords();
		themewords = doc.getThemewords();
		summary = doc.getSummary();
		note = doc.getNote();
		language = doc.getLanguage();
		lastsearchtime = doc.getLastsearchtime();
		createtime = doc.getCreatetime();
		edittime = doc.getEdittime();	
		sharetg = doc.getSharetg();
	}
	public boolean UpdateDocument()
	{
		boolean flag = false;
		Document doc = DBOperate.getDocument(documentid);
		return  flag;
		
	}
	
	public void validateInput(FacesContext context,   
	        UIComponent component, Object value){
		String text = value.toString();  
	    if(text.equals("")){  
	        throw new ValidatorException(new FacesMessage(" 内容不能为空！"));
	    }  
	}
	

}
